package com.example.healthapp.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.example.healthapp.R;
import com.example.healthapp.ui.activity.MainActivity;

// 通知工具类
public class MessageNotification {

    private static final String CHANNEL_ID = "777";
    private static final String CHANNEL_NAME = "message777";
    private static final int NOTIFICATION_ID = 777;

    public static void sendNotification(Context context, String title, String message) {
        //获取通知管理类
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //版本适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //通知管道
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(getContentIntent(context)) // 设置点击通知时启动的操作
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

    public static PendingIntent getContentIntent(Context context) {
        Intent intent = new Intent(context, MainActivity.class); // 替换为您想要启动的活动
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    }
}